"""
对日志的处理，创建一个类Logger类，实例化该类就可以得到一个配置好的日志记录器对象
"""
import logging
import os
import time

class Logger(object):
    _instance = None
    def __new__(cls, *args, **kwargs):
        if Logger._instance is None:
            _logger = logging.getLogger("auto_test")
            _logger.setLevel(logging.DEBUG)
            sh = logging.StreamHandler()
            sh.setLevel(logging.WARNING)
            # 处理日志文件的路径
            current_path = os.path.abspath(__file__)
            root_path = os.path.dirname(
                os.path.dirname(
                    current_path
                )
            )
            log_dir = os.path.join(root_path, "log")
            # 判断log_dir是否存在，如果不存在，则创建
            if not os.path.isdir(log_dir):
                os.mkdir(log_dir)
            # 处理日志文件的名称
            log_name = time.strftime("%Y_%m_%d.log")
            log_path = os.path.join(log_dir, log_name)
            fh = logging.FileHandler(filename=log_path, encoding="utf-8")
            fh.setLevel(logging.DEBUG)
            fm = logging.Formatter(
                fmt="[%(levelname)s-%(asctime)s-%(module)s-%(lineno)d]: %(message)s "
            )
            sh.setFormatter(fm)
            fh.setFormatter(fm)
            _logger.addHandler(sh)
            _logger.addHandler(fh)
            Logger._instance = _logger

        return Logger._instance

